//
//  JCommentView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/30.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JCommentView.h"

@implementation JCommentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.opaque = NO;
        self.backgroundColor = [UIColor whiteColor];
        [self setCommentView];
    }
    return  self;
}

- (void)setCommentView
{
    CALayer *headImg = [CALayer layer];
    headImg.frame = CGRectMake(10, 5, 30, 30);
    headImg.cornerRadius = 15;
    headImg.masksToBounds = YES;
    headImg.backgroundColor = [UIColor grayColor].CGColor;
    [self.layer addSublayer:headImg];
    _headImg = headImg;
    
    _nameLa = [YYLabel new];
    _nameLa.top = 0;
    _nameLa.left = headImg.right + 10;
    _nameLa.size = CGSizeMake(kWindowW - 50 - 10, 40);
    _nameLa.font = kFONT_TITLE(16);
    _nameLa.textColor = [UIColor blackColor];
    [self addSubview:_nameLa];
    
    _timeLa = [YYLabel new];
    _timeLa.top = _nameLa.bottom + 5;
    _timeLa.left = 10;
    _timeLa.size = CGSizeMake(kWindowW - 20, 20);
    _timeLa.font = kFONT_TITLE(14);
    _timeLa.textColor = kRGBColor(150, 150, 150);
    [self addSubview:_timeLa];
    
    _contentLa = [YYLabel new];
    _contentLa.top = _timeLa.bottom + 5;
    _contentLa.left = 10;
    _contentLa.size = CGSizeMake(kWindowW - 20, 40);
    _contentLa.font = kFONT_TITLE(14);
    _contentLa.textColor = kRGBColor(104, 104, 104);
    _contentLa.numberOfLines = 0;
    [self addSubview:_contentLa];
    
}


- (void)setCommentModel:(JProductComment *)commentModel
{
    _commentModel = commentModel;
    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:commentModel.HeadImg]]];
    _headImg.contents = (__bridge id _Nullable)(img.CGImage);
    _nameLa.text = commentModel.CustomerName;
    _timeLa.text = commentModel.CommentDateText;
    _contentLa.text = commentModel.Content;
    
    CGSize sizeLable = [_contentLa sizeThatFits:CGSizeMake(kWindowW - 20, 1000)];
    _contentLa.size = sizeLable;
    
    [self setUpStarNumber:commentModel.Rating];
}

- (void)setUpStarNumber:(NSInteger)num
{
    for (int i = 0; i < 5; i++) {
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(kWindowW - (5 - i) * 12 - 12, CGRectGetMaxY(_nameLa.frame) +4, 12, 12);
        [self.layer addSublayer:layer];
        
        if (i < num)
        {
            layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"redstar"].CGImage);
        }
        else
        {
            layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"graystar"].CGImage);
        }
        
    }
}

- (CGFloat)getViewHeight
{
    CGFloat height = CGRectGetHeight(_nameLa.frame) + CGRectGetHeight(_timeLa.frame) + CGRectGetHeight(_contentLa.frame) + 25;
    return height;
}

@end
